package JiChu;

/**
 * ThreadLocal:
 *      1.set() : 将变量绑定到当前线程中
 *      2.get() : 获取当前线程绑定的变量
 */
public class ThreadLocalDemo {

    ThreadLocal<String> tl = new ThreadLocal<>();

    //变量
    private String content;

    private String getContent() {
        //return content;
        String s = tl.get();
        return s;
    }

    private void setContent(String content) {
        //this.content = content;
        //将Content绑定到当前线程
        tl.set(content);
    }

    public static void main(String[] args) {
        ThreadLocalDemo demo = new ThreadLocalDemo();

        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    /*
                        每个线程：存一个变量，过一会取出这个变量
                     */
                    demo.setContent(Thread.currentThread().getName() + "的数据");
                    System.out.println("-------------------------");
                    System.out.println(Thread.currentThread().getName() + "--->" + demo.getContent());
                }
            });

            thread.setName("线程" +i);
            thread.start();
        }
    }
}
